﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Text.RegularExpressions;
using Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ChBusiness.Struct;
using System.Net;
using Common.Log;
using WF_DataAccess;
using WF_Business;

namespace ChBusiness.Common
{
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class SystemLogs
    {
        private Database dbGloab = BasicOperate.CreateNetOfficeDataBase();

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="strUserID">操作人</param>
        /// <param name="strOptype">操作类型(add,update,delete)</param>
        /// <param name="strRemark">操作描述</param>
        /// <param name="strOpSql">操作SQL语句</param>
        /// <!--addby zhongjian 20100331-->
        public static void AddSystemLogs(string strUserID, string strOptype,string strRemark,string strOpSql)
        {
            //添加操作日志
            Database dbTemp = BasicOperate.CreateNetOfficeDataBase();
            string strSql = string.Empty;
            string myIP = string.Empty;
            strOpSql = strOpSql.Replace("'", "");
            try
            {
                System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
                //获取登录者ip地址
                myIP = addressList[0].ToString();
                strSql = string.Format(@"insert into xt_operationlogs (USERID,OPTTYPE,REMARK,IP,OPTSQL,OPTID) values 
                                    ('{0}','{1}','{2}','{3}','{4}','2')", strUserID, strOptype, strRemark, myIP, strOpSql);
                dbTemp.ExecuteScalar(CommandType.Text, strSql);
            }
            catch (Exception ex)
            {
                strRemark = " 外网在写入操作日志时发生错误: " + ex.ToString();

                ILogger ilogger = LoggerFactory.GetLogger();
                ilogger.WriteInfo("OfficeWeb", "ShowError", strRemark);
            }
        }

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="strUserID">操作人</param>
        /// <param name="strOptype">操作类型(add,update,delete)</param>
        /// <param name="strRemark">操作描述</param>
        /// <param name="strOpSql">操作SQL语句</param>
        /// <param name="strIP">操作者客户端IP</param>
        public static void AddSystemLogs(string strUserID, string strOptype, string strRemark, string strOpSql, string strIP)
        {
            try
            {
                IDataParameter[] idParms = DataFactory.GetParameter(DatabaseType.Oracle, 6);
                idParms[0].ParameterName = "struserid";//操作人
                idParms[0].Value = strUserID;
                idParms[1].ParameterName = "stroptype";//操作类型(add,update,delete)
                idParms[1].Value = strOptype;
                idParms[2].ParameterName = "strremark";//操作描述
                idParms[2].Value = strRemark;
                idParms[3].ParameterName = "strip";//客户端ip
                idParms[3].Value = strIP;
                idParms[4].ParameterName = "stropsql";//操作SQL语句
                strOpSql = strOpSql.Replace("'", "");
                idParms[4].Value = strOpSql;
                idParms[5].ParameterName = "stropid";//操作系统(1:内网审批;2:外网申报)
                idParms[5].Value = "1";
                SysParams.OAConnection().RunProc("ProcOperationlogs", ref idParms);
            }
            catch (Exception ex)
            {
                strRemark = " 外网在写入操作日志时发生错误: " + ex.ToString();

                ILogger ilogger = LoggerFactory.GetLogger();
                ilogger.WriteInfo("OfficeWeb", "ShowError", strRemark);
            }
        }

        #region 查询操作日志列表
        /// <summary>
        /// 查询操作日志
        /// </summary>
        /// <param name="type">日志类型</param>
        /// <returns></returns>
        public static DataTable Query(string UserName, int OptType, string action, string remark, DateTime DtStart, DateTime DtEnd)
        {
            IDataAccess da = SysParams.OAConnection();
            DataTable dt = null;

            string sql = "select ID,userid,OptTime,OptType,OptAction,Remark,IP,UserMemo from XT_OperationLogs where opttime between to_date('" + DtStart + "','YYYY-MM-DD hh24:mi:ss') and to_date('" + DtEnd + "','YYYY-MM-DD hh24:mi:ss') ";

            if (UserName != "") //操作人
            {
                sql += "  and userid LIKE '%" + UserName + "%'";
            }
            if (!String.IsNullOrEmpty(action))
            {
                sql += " AND optAction LIKE '%" + action + "%'";
            }
            if (!String.IsNullOrEmpty(remark))
            {
                sql += " AND Remark LIKE '%" + remark + "%'";
            }
            if (OptType != 0) //操作类型
            {
                sql += "  and opttype='" + OptType + "'";
            }
            da.RunSql(sql, out dt);
            return dt;
        }
        #endregion

        #region 删除日志
        /// <summary>
        ///  删除日志
        /// </summary>
        /// <param name="id">日志ID</param>
        public static void Delete(string id)
        {
            string sql = "";
            IDataAccess da = SysParams.OAConnection();

            sql = "delete from XT_OperationLogs where id='" + id + "'";
            da.RunSql(sql);
            da.Close();
        }
        #endregion

        /// <summary>
        /// 添加系统日志
        /// </summary>
        /// <param name="czy"></param>
        /// <param name="czdz"></param>
        /// <param name="czsjid"></param>
        /// <param name="strIp">Ip地址</param>
        public void Inputlog(string czy, string czdz, string czsjid, string strIp)
        {
            string strSql = string.Format(@"insert into sys_log(czy,czrq,czdz,czsjid,ip) 
                values('{0}',sysdate,'{1}','{2}','{3}')", czy, czdz, czsjid, strIp);
            dbGloab.ExecuteNonQuery(CommandType.Text, strSql);
        }

        /// <summary>
        /// 添加系统日志
        /// </summary>
        /// <param name="czy"></param>
        /// <param name="czdz"></param>
        /// <param name="czsjid"></param>
        public void Inputlog(string czy, string czdz, string czsjid)
        {
            Inputlog(czy, czdz, czsjid, string.Empty);
        }

    }
}
